package middleware

import (
	"gitee.com/pearl-zz/go-manage-system-core/sdk"
	"gitee.com/pearl-zz/go-manage-system-core/sdk/pkg/jwtauth"
	"gitee.com/pearl-zz/go-manage-system/common/actions"
	"github.com/gin-gonic/gin"
)

const (
	JwtTokenCheck   string = "JwtToken"
	RoleCheck       string = "AuthCheckRole"
	PermissionCheck string = "PermissionAction"
)

func InitMiddleware(r *gin.Engine) {
	// 数据库连接
	r.Use(WithContextDb)
	// 日志处理
	r.Use(LoggerToFile())
	// 自定义错误处理
	r.Use(CustomError)

	// NoCache is a middleware function that appends headers
	r.Use(NoCache)

	// 跨域处理
	r.Use(Options)
	// Secure is a middleware function that appends security
	r.Use(Secure)

	sdk.Runtime.SetMiddleware(JwtTokenCheck, (*jwtauth.GinJwtMiddleware).MiddlewareFunc)
	sdk.Runtime.SetMiddleware(RoleCheck, AuthCheckRole())
	sdk.Runtime.SetMiddleware(PermissionCheck, actions.PermissionAction())
}
